package com.sz.common.core.system.service.impl;

import com.sz.common.base.dao.Pagination;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.system.entity.OperationLog;
import com.sz.common.core.system.service.OperationLoggerService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Function: OperationLoggerServiceImpl <br>
 * Author: Charles <br>
 * Date: 2016-11-01 09:48:00
 */
@Service("operationLoggerService")
public class OperationLoggerServiceImpl extends AbstractService implements OperationLoggerService {

    /**
     * 添加操作日志.
     *
     * @param accountId   账户Id
     * @param domainId    域Id
     * @param moduleName  模块名称
     * @param subModule   子模块名称
     * @param action      action
     * @param operation   操作日志
     * @param dataBackup  备份数据
     * @param ip          IP地址
     * @param level       级别
     * @param description 描述
     */
    @Override
    public void addOperationLog(int accountId, int domainId, String moduleName, String subModule, String action,
                                String operation, String dataBackup,
                                String ip, int level, String description) {
        if (operation == null || operation.trim().length() == 0) {
            throw new IllegalArgumentException("operation cannot be empty!");
        }
        OperationLog log = new OperationLog();
        log.setAccountId(accountId);
        log.setDomainId(domainId);
        log.setModule(moduleName);
        log.setSubModule(subModule);
        log.setAction(action);
        log.setOperation(operation);
        log.setDataBackup(dataBackup);
        log.setUserIP(ip);
        log.setLevel(level);
        log.setDescription(description);
        this.addOperationLog(log);
    }

    @Override
    public void addOperationLog(OperationLog log) {
        if (log != null) {
            dao.save(this.getSqlName("insertSelective"), log);
        }
    }

    /**
     * 查询日志
     *
     * @param accountId  --账户Id，可选
     * @param domainId   --域Id，可选
     * @param moduleName --模块名称，模糊查询，可选
     * @param subModule  --子模块名称，模糊查询，可选
     * @param action     --action,模糊查询，可选
     * @param keyword    --关键字，模糊查询，可选
     * @param userIP     --IP，模糊查询，可选
     * @param level      --level，可选
     * @param desc       --描述，模糊查询，可选
     * @param dateStart  --开始时间，可选
     * @param dateEnd    --结束时间，可选
     * @param pageIndex  --页码
     * @param pageSize   --页面大小
     * @return
     */
    @Override
    public ResultDto findByFilterListPage(int accountId, int domainId, String moduleName, String subModule,
                                          String action, String keyword, String userIP, int level, String desc,
                                          Date dateStart, Date dateEnd, int pageIndex, int pageSize) {

        ParamData params = new ParamData();
        if (accountId > 0) {
            params.put("accountId", accountId);
        }
        if (domainId > 0) {
            params.put("domainId", domainId);
        }
        if (moduleName != null && moduleName.trim().length() > 0) {
            params.put("model", moduleName);
        }
        if (subModule != null && subModule.trim().length() > 0) {
            params.put("subModule", subModule);
        }
        if (action != null && action.trim().length() > 0) {
            params.put("action", action);
        }
        if (keyword != null && keyword.trim().length() > 0) {
            params.put("operation", keyword);
        }
        if (userIP != null && userIP.trim().length() > 0) {
            params.put("userIP", userIP);
        }
        if (level > 0) {
            params.put("level", level);
        }
        if (desc != null && desc.trim().length() > 0) {
            params.put("description", desc);
        }
        if (dateStart != null) {
            params.put("dateStart", dateStart);
        }
        if (dateEnd != null) {
            params.put("dateEnd", dateEnd);
        }

        Pagination page = new Pagination();
        page.setCurrentPage(pageIndex);
        page.setPageSize(pageSize);
        params.setPagination(page);
        List<OperationLog> logs = dao.findForList(this.getSqlName("findByFilterListPage"), params,
                OperationLog.class);
        ResultDto result = new QResultDto(logs, page);
        return result;
    }

    /**
     * 删除日志
     *
     * @param ids --日志ID
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        dao.delete(this.getSqlName("deleteBatch"), ids);
    }

    /**
     * 获取日志
     *
     * @param id --日志ID
     * @return 日志
     */
    @Override
    public OperationLog getOperationLog(Long id) {
        return (OperationLog) dao.findForObject(this.getSqlName("selectByPrimaryKey"), id);
    }

    @Override
    protected String getMapperNamespace() {
        return "OperationLogMapper";
    }
}
